﻿// ע�

using CodingSeb.ExpressionEvaluator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XT.Common.Extensions;

namespace IotClientService.Extensions;
/// <summary>
/// 表达式扩展
/// </summary>
[System.Security.SecuritySafeCritical]
public static class ExpressionEvaluatorExtension
{
    static ExpressionEvaluator ExpressionEvaluator;
   
    static ExpressionEvaluatorExtension()
    {
        ExpressionEvaluator = new();
        ExpressionEvaluator.Variables = new Dictionary<string, object>()
            {             
              { "raw", ""},
            };

    }
    /// <summary>
    /// 计算表达式：例如：raw*100，raw为原始值
    /// </summary>
    public static object GetExpressionsResult(this string expressions, object rawvalue)
    {
        if (expressions.IsNullOrEmpty() || !expressions.Contains("raw"))
        {
            return rawvalue;
        }
        ExpressionEvaluator.Variables["raw"] = rawvalue;
    
        var value = ExpressionEvaluator.Evaluate(expressions);
        return value;
    }


  
}
